import request from "@/utils/request";

const defaultApi = (apiName) => {
    return {
        list: (params) => request.get(`/${apiName}/list`, { params }),
        simpleList: (params) => request.get(`/${apiName}/simpleList`, { params }),
        page: (params) => request.get(`/${apiName}`, { params }),
        read: (id) => request.get(`/${apiName}/` + id),
        create: (data) => request.post(`/${apiName}`, data),
        update: (id, data) => request.put(`/${apiName}/` + id, { ...data, create_time: undefined, id: undefined }),
        delete: (id) => request.delete(`/${apiName}/` + id)
    }
}

export const http = {
    // 认证相关接口
    auth: {
        // 注册
        register: (data) => request.post('/auth/register', data),
        // 登录
        login: (data) => request.post('/auth/login', data),
        // 获取当前登录用户信息
        getUserInfo: (username) => request.get('/auth/info', { params: { username } })
    },

    // 仪表盘接口
    dashboard: {
        // 获取学生首页
        getStudentDashboard: (userId) => request.get(`/dashBoard/${userId}`)
    },

    // 学习资源接口
    resource: {
        // 创建学习资源
        create: (data) => request.post('/resource', data),
        // 更新学习资源
        update: (data) => request.put('/resource', data),
        // 删除学习资源
        delete: (id) => request.delete(`/resource/${id}`),
        // 获取学习资源详情
        getDetail: (id) => request.get(`/resource/${id}`),
        // 分页查询学习资源列表
        page: (params) => request.get('/resource/page', { params }),
        // 增加浏览次数
        increaseView: (id) => request.put(`/resource/${id}/view`),
        // 增加下载次数
        increaseDownload: (id) => request.put(`/resource/${id}/download`)
    },
    resourcePath: {
      // learning-path
      page: (params) => request.get('/learning-path/page', { params }),
      create: (data) => request.post('/learning-path', data),
      update: (data) => request.put('/learning-path', data),
      delete: (id) => request.delete(`/learning-path/${id}`),
    },
    // 知识点
    knowledgeNode: {
        // 创建知识点
        create: (data) => request.post('/knowledge/node', data),
        // 更新知识点
        update: (data) => request.put('/knowledge/node', data),
        // 删除知识点
        delete: (id) => request.delete(`/knowledge/node/${id}`),
        // 分页查询知识点列表
        page: (params) => request.get('/knowledge/nodes', { params })
    },
    // 知识点关系
    knowledgeRelation: {
        // 创建知识点关系
        create: (data) => request.post('/knowledge/relation', data),
        // 更新知识点关系
        update: (data) => request.put('/knowledge/relation', data),
        // 删除知识点关系
        delete: (id) => request.delete(`/knowledge/relation/${id}`),
        // 分页查询知识点关系列表
        page: (params) => request.get('/knowledge/relations', { params })
    },
    // 学习记录接口
    learning: {
        // 新增/更新学习记录
        save: (data) => request.post('/user-learning', data),
        // 删除学习记录
        delete: (id) => request.delete(`/user-learning/${id}`),
        // 获取学习记录详情
        getDetail: (id) => request.get(`/user-learning/${id}`),
        // 分页查询学习记录列表
        page: (params) => request.get('/user-learning/page', { params }),
        // 查询用户的学习记录列表
        getUserLearning: (userId) => request.get(`/user-learning/user/${userId}`),
        // 获取课程学习记录
        getResourceLearning: (resourceId) => request.get(`/user-learning/resource/${resourceId}`)
    },

    // 消息接口
    message: {
        // 发送消息
        send: (data) => request.post('/messages/send', data),
        getChatFriends: (userId) => request.get(`/messages/chat-friends/${userId}`),
        // 获取未读消息
        getUnread: (senderId, receiverId) => request.get(`/messages/unread/${senderId}/${receiverId}`),
    },
    user: {
        ...defaultApi('user'),
        getUserInfo: (userId) => request.get(`/user/getUserInfo`, { params: { userId }}),
        getUserRoles: (userId) => request.get(`/user/getUserRoles`, { params: { userId }}),
        updateUserRoles: (data) => request.post(`/user/updateUserRoles`, data),
    },
    // 学习路径接口
    learningPath: {
        // 获取路径节点列表
        getPathNodes: (pathId) => request.get(`/learning-path/getPathNodeByPathId/${pathId}`),
        // 添加路径节点
        addPathNode: (data) => request.post('/learning-path/pathNode', data),
        // 更新路径节点
        updatePathNode: (data) => request.put(`/learning-path/pathNode`, data),
        // 删除路径节点
        deletePathNode: (id) => request.delete(`/learning-path/pathNode/${id}`)
    },
}

// 文件上传
export const uploadFile = (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return request({
        url: '/files/upload',
        method: 'post',
        data: formData
    })
}
